import math

# 45% （时间超10s了）
# 只考虑到了对条件一进行优化，没有考虑到对对角线查找（条件二）的情况进行优化。

def solution1():
    n, m = map(int, input().split())
    records = []
    for i in range(1002):
        record_list = list()
        records.append(record_list)
    matrix = []
    for i in range(n):
        row_list = list(map(int, input().split()))
        matrix.append(row_list)
    # print(matrix.__str__())
    # print(len(records))
    for x in range(n):
        for y in range(m):
            records[matrix[x][y]].append((x, y))
    # print(records)

    count = 0
    for i in range(1002):
        for x in range(len(records[i])-1):
            for y in range(x+1, len(records[i])):
                x_1 = records[i][x][0]
                y_1 = records[i][x][1]
                x_2 = records[i][y][0]
                y_2 = records[i][y][1]
                dis_x = int(math.fabs(x_1-x_2))
                dis_y = int(math.fabs(y_1-y_2))
                if dis_x == dis_y and dis_x > 0:
                    count += 1
    print(2 * count)
    pass


if __name__ == '__main__':
    solution1()